var app = angular.module('qzchannelApp', []);
var keyTip="请输入待检索的关键字";


$("#status").select2({
	 placeholder: "请选择菜单状态",
	 allowClear: true
});

app.controller('qzchannelController', function($scope, $http, MenuService) {
	$scope.load=function(){
		MenuService.list($scope.sel).success(function (response) {
			$scope.pageInfo=response.data;
	    });
	}
	
	$scope.load=function(isRest){
		$scope.sel=$scope.sel?$scope.sel:{};
		var pageNum=$scope.pageInfo?isRest==false?$scope.pageInfo.pageNum:1:1;
		var pageSize=$scope.pageInfo?$scope.pageInfo.pageSize:10;
		$scope.sel.pageNum=pageNum;
		$scope.sel.pageSize=pageSize;
		
		var param={};  
        for(var k in $scope.sel){  
        	param[k] = $scope.sel[k];  
        	if(k=="key"&&$scope.sel[k]==keyTip){
        		delete param[k];
        	}
        
        }
        
        MenuService.list(param).success(function (response) {
			$scope.pageInfo=response.data;
			$scope.pageInfo.pageLinks=getPageLinks(response.data.pageNum,response.data.pages);
	    });
	}
	
	$scope.load();
    $scope=pagination.init($scope);
    $scope.qzchannelCreator=function(){
    	redirectUrl("manage/channel/","add.jsp");
    }
    $scope.qzchanneledit=function(){
    	var id = $("input[type='radio']:checked").val();
    	//document.location.href='manage/channel/edit.jsp?id=' + id;
    	redirectUrl("manage/channel/","edit.jsp?id="+ id);
    }
    $scope.qzchanneldetail=function(){
    	var id = $("input[type='radio']:checked").val();
    	//document.location.href='manage/channel/details.jsp?id=' + id;
    	redirectUrl("manage/channel/","details.jsp?id="+ id);
    	
    }
    $scope.refreshData=function(){
    	$scope.load();
    }
    
    $scope.qzchanneldel=function(id){
    	$scope.delItem={id:id};
    	$('#box-del').modal({
    		show: true
    	});
    	
    }
    $scope.del=function(id){
    	MenuService.del(id).success(function (response) {
    		$('#box-del').modal('hide');
    		//$scope.load();
    		$scope.msg=response.meta;
    		if($scope.msg.message == "error") {
    			$('#box-msg').modal({
    	    		show: true
    	    	});
    		}else {
    			redirectUrl("manage/channel/","list.jsp");
    		}
    	});
    }
    if(inputPlaceholder()){
    	$scope.sel.key=keyTip;
    }

});

app.factory('MenuService', function ($http) {
	var list = function (param) {
        return $http.post('qz/channel/getByPage',param);
    };
    var del = function (id){
    	return $http.put('qz/channel/del/'+id);
    }
	return {
		list:function(param){
			return list(param);
		},
		del:function(id){
			return del(id);
		}
	}
});
